package LevelParser;

import java.io.FileInputStream;
import java.util.ArrayList;

public class levelParser {
	protected boolean initialised = true;			
	protected String level = new String();		//xml in one string
	protected ArrayList<String> tag = new ArrayList<String>();	//xml in string array by tags/contents
	
	public levelParser(String XMLType,String xmlpath)
		{
			
		try {
			//reading to String
			FileInputStream in = new FileInputStream(xmlpath);
			
			 try {
			        int c;
			        while ((c = in.read()) != -1) 
			        	{
			            level+=(char)c;
			        	}
			    } 
			 finally {
			        if (in != null)
			            in.close();
			        }
			 
			 //tag seperation
				
			int start=0;
			boolean inside = false;
				//whitespaces filtering
			String l=level.replaceAll("\\s","");

			for(int j=0;j<l.length();j++)
				{
				if(!inside && l.charAt(j)==60)
					{
						tag.add(l.substring(start,j));
						inside=true;
						start=j;
					}
				else if(inside && l.charAt(j)==62)
					{
						inside=false;
						tag.add(l.substring(start,j+1));
						if(j<l.length()-1)start=j+1;
					}
				}
			
			
			//whiterecords filtering
			while(tag.remove("")){}
			
			//checking if correct levelMap
			if(!((tag.get(0).equals("<"+XMLType+">") || tag.get(1).equals("<"+XMLType+">"))  && tag.get(tag.size()-1).equals("</"+XMLType+">")))
				{
					System.out.println("Wrong XML "+XMLType+" file.");
					initialised = false;
				}
			else System.out.println("XML "+XMLType+" from "+xmlpath+" file read successfully.");
							
			}
		catch(Exception e){System.out.print("XML "+XMLType+" read failed."); initialised = false;}
		}
	
	
	//Note: while reading, if concrete attribute is not set in xml, value is set to null
	public String[][] GetAttributes(String TypeOfData, String[] AttrTypes)
		{
		int TODnumber=Math.min(count("<"+TypeOfData+">",tag),count("</"+TypeOfData+">",tag));
		if(TODnumber==0)return null;
		
		String[][] TODAtts = new String[TODnumber][AttrTypes.length];	
			
		if(!initialised)return null;
			
		int e=0;
		
		for(int i=0;i<tag.size()-2;i++)	//enemy parse
			{
			if(tag.get(i).equals("<"+TypeOfData+">")) //found enemy - starting comparing attributes names until end of enemy found
				{
				TODAtts[e][0]=Integer.toString(e);
				while(!tag.get(i).equals("</"+TypeOfData+">") && i<tag.size())
					{
					for(int j=0;j<AttrTypes.length;j++)	
						if(tag.get(i).equals("<"+AttrTypes[j]+">") && tag.get(i+2).equals("</"+AttrTypes[j]+">"))
							{
								TODAtts[e][j]=tag.get(i+1);
							}
					i++;
					}
				e++;
				}
			if(e>TODnumber)break;
				
			}
			
		return TODAtts;
		}
	
	
	//Note: while reading, if concrete attribute is not set in xml, value is set to null
	public String[] GetSameNameAttributes(String TypeOfData, String AttrType)
		{
			int ATnumber=Math.min(count("<"+AttrType+">",tag),count("</"+AttrType+">",tag));
			if(ATnumber==0)return null;
			
			String[] ATAtts = new String[ATnumber];	
				
			if(!initialised)return null;
				
			int e=0;
			
			for(int i=0;i<tag.size()-2;i++)	//enemy parse
				{
				if(tag.get(i).equals("<"+TypeOfData+">")) //found enemy - starting comparing attributes names until end of enemy found
					{
					while(!tag.get(i).equals("</"+TypeOfData+">") && i<tag.size())
						{
							if(tag.get(i).equals("<"+AttrType+">") && tag.get(i+2).equals("</"+AttrType+">"))
								{
									ATAtts[e]=tag.get(i+1);
									e++;
								}
						i++;
						}
					}
				}
				
			return ATAtts;
			}
	
	
	protected int count(String s,ArrayList<String> tag)
		{
			int x=0;
			for(int i=0;i<tag.size();i++)
				if(tag.get(i).equals(s))
					x++;
			return x;
		}
	
	public void showXML()
		{
			if(!initialised)return;
				System.out.println(level);
			return;
		}
	
	public void showTags()
		{
			if(!initialised)return;
			for(int i=0;i<tag.size();i++)
				System.out.println(tag.get(i));
			return;
		}
	
}
